//
//  FamousCigaretteViewController.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/25.
//  Copyright © 2016年 黄威. All rights reserved.
//


#import "FamousCigaretteViewController.h"
#import "Masonry.h"
#import "TitleView.h"
#import "FirstViewControllerOfFamous.h"
#import "SecondViewControllerOfFamous.h"
#import "ThirdViewControllerOfFamous.h"
#import "FourthViewControllerOfFamous.h"
#import "TAMacro.h"
#import "SearchCigaretteTableViewController.h"
@interface FamousCigaretteViewController()<UIScrollViewDelegate>
@property (nonatomic, strong) TitleView *titleView;
@property (nonatomic, strong) UIScrollView *scrollViewOfMain;
@end

@implementation FamousCigaretteViewController

- (void)viewWillAppear:(BOOL)animated{
 self.tabBarController.tabBar.hidden = NO;

}

-(void)viewDidLoad{
  
    [self createNavigationItem];
    [self createScrollView];
}

#pragma mark - 创建scrollView

- (void)createScrollView{
    
    self.scrollViewOfMain = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollViewOfMain.contentSize = CGSizeMake(self.view.bounds.size.width * 4, self.view.bounds.size.height );
    [self.view addSubview:self.scrollViewOfMain];
    self.scrollViewOfMain.pagingEnabled = YES;
    self.scrollViewOfMain.delegate = self;
    
   
    FirstViewControllerOfFamous * firstView = [[FirstViewControllerOfFamous alloc]initWithStyle:UITableViewStylePlain];
    SecondViewControllerOfFamous *secindView = [[SecondViewControllerOfFamous alloc]initWithStyle:UITableViewStylePlain];
    ThirdViewControllerOfFamous *thirdView = [[ThirdViewControllerOfFamous alloc]initWithStyle:UITableViewStylePlain];
    FourthViewControllerOfFamous *fouthView = [[FourthViewControllerOfFamous alloc]initWithStyle:UITableViewStylePlain];
    
    [self.scrollViewOfMain addSubview:firstView.view];
    [self addChildViewController:firstView];
    firstView.view.frame = CGRectMake(0, 0, (self.view.bounds.size.width),self.view.bounds.size.height - 49 - 49 );
    
    
    [self.scrollViewOfMain addSubview:secindView.view];
    [self addChildViewController:secindView];
    secindView.view.frame =  CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width,self.view.bounds.size.height - 49 - 49);
    
    [self.scrollViewOfMain addSubview:thirdView.view];
    [self addChildViewController:thirdView];
    thirdView.view.frame =  CGRectMake(self.view.bounds.size.width * 2, 0, self.view.bounds.size.width,self.view.bounds.size.height - 49 - 49);
    
    [self.scrollViewOfMain addSubview:fouthView.view];
    [self addChildViewController:fouthView];
    fouthView.view.frame =  CGRectMake(self.view.bounds.size.width * 3, 0, self.view.bounds.size.width,self.view.bounds.size.height - 49 - 49);
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(execute:)
                                                 name:@"test"
                                               object:nil];
    
    
    //防止navigation遮挡视图
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
   
}

- (void)createNavigationItem{
    
    
    //设置navigation相关属性
    
   
    
    self.titleView = [[TitleView alloc]init];
    self.titleView.frame = CGRectMake(0, 0, (SCREEN_SIZE_WIDTH_ME * 0.8 ), 44);
    self.navigationItem.titleView = self.titleView;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(search)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
}

- (void)search{

    SearchCigaretteTableViewController *search = [[SearchCigaretteTableViewController alloc]init];
    
    [self.navigationController pushViewController:search animated:YES];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //延迟发送通知
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_FanPage"
                                                            object:self.scrollViewOfMain];
    });
    
    
}


- (void)execute:(NSNotification *)noti{
    
    UIView *view = [noti object];
    NSInteger a = view.frame.origin.x;
    
    
    if (a == 0) {
        
        [self.scrollViewOfMain setContentOffset:CGPointMake(0, 0)];
    }else if(a == 51 || a == 60 || a == 66){
    
    [self.scrollViewOfMain setContentOffset:CGPointMake(self.view.bounds.size.width, 0)];
    
    }else if(a == 102 || a == 120 || a == 132){
        
        [self.scrollViewOfMain setContentOffset:CGPointMake(self.view.bounds.size.width * 2, 0)];
        
    }else if(a == 198 || a == 180 || a == 153){
        
        [self.scrollViewOfMain setContentOffset:CGPointMake(self.view.bounds.size.width *3 , 0)];
        
    }

    
}

-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];

}


@end
